package com.jcb.countfrench.view;



import android.content.Context;
import android.media.MediaPlayer;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jcb.countfrench.R;
import com.jcb.countfrench.model.BasicData;
import com.jcb.countfrench.model.User;
import com.jcb.countfrench.presenter.CountPresenter;

public class LearnView extends LinearLayout{
	private Button  buttonPrev, buttonFirst, buttonNext, buttonRepeteSon, buttonLast ;  
	private Handler mHandler = new Handler();
	BasicData currentData ;
	private String idLearning = "1-10" ;
	TextView textLearnNumber ;
	private CountPresenter countPresenter ;
	MediaPlayer mediaPlayer ;
	private Runnable runnable ;
	
	
	
	public void setMediaPlayer(MediaPlayer mediaPlayer) {
		this.mediaPlayer = mediaPlayer;
	}


	public void setCountPresenter(CountPresenter countPresenter) {
		this.countPresenter = countPresenter;
	}


	public LearnView(Context context,  User user ) {
		super(context);
		LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layoutInflater.inflate(R.layout.learnview,this);		
		   buttonPrev  = (Button) view.findViewById(R.id.btnlearnPrev);
		   buttonFirst  = (Button) view.findViewById(R.id.btnlearnFirst);
	       buttonRepeteSon = (Button) view.findViewById(R.id.btnlearnRepeat);
	       buttonNext		= (Button) view.findViewById(R.id.btnlearnNext);		
	       textLearnNumber = (TextView) view.findViewById(R.id.textLearnNumber);
	       buttonLast		= (Button) view.findViewById(R.id.btnlearnLast);
	       
	      // currentData = presenter.firstItem() ;
		
		buttonNext.setOnClickListener(new View.OnClickListener() {
	           public void onClick(View v) {
	        	   next();
	           }
	       });		
		
		buttonPrev.setOnClickListener(new View.OnClickListener() {
	           public void onClick(View v) {
	               Log.d("TAG", "mAddAccountButton clicked");
	               currentData = countPresenter.prevItem(currentData);
	               display(currentData);
	           }
	       });		
		
		buttonLast.setOnClickListener(new View.OnClickListener() {
	           public void onClick(View v) {
	               Log.d("TAG", "mAddAccountButton clicked");
	               currentData = countPresenter.lastItem(idLearning);
	               display(currentData);
	           }
	       });		

		
	}
	
	public void next() {
        Log.d("TAG", "mAddAccountButton clicked");
        if (currentData != null) {
        	currentData = countPresenter.nextItem(currentData);
        	if (currentData != null) {
        		display(currentData);
        	}
        }
	}
	
	public void initialDisplay() {
		currentData = countPresenter.firstItem(idLearning) ;
		display(currentData) ;		
		runnable = new Runnable() {
		    public void run() {
		    	 next();
		    	 mHandler.postDelayed(this, 5000);    
		    }
		};
		mHandler.postDelayed(runnable, 5000);	
	}
	
	
	public void display(BasicData currentData) {		
		if (currentData != null) {
			textLearnNumber.setText(currentData.getLibLangRef());
			int sound = currentData.getSoundLangRef();
			Log.i("WXW", " lib lang " + currentData.getLibLangRef() + " sound=" + sound);				
			if (sound > 0) {
				mediaPlayer.reset();
				mediaPlayer = MediaPlayer.create(this.getContext(), currentData.getSoundLangRef());
				mediaPlayer.start();
			}		
		}
	}
	
	
	
	

}
